#include "infobox.h"
#include "genomeelement.h"
#include "multigeneregionviswidget.h"
#include "BaseDataManagement/genomeelementdatapacket.h"

//---------------------------------------------------------------------------------------------------------------------------------------
//INFO TAB-------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------

InfoTab::InfoTab(GenomeElementDataPacket* d, QWidget *parent)
    : QWidget(parent), dataToDisplay(d)
{
    //fileNameLabel = new QLabel(textToDisplay);

//    mainLayout = new QVBoxLayout;
//    //mainLayout->addWidget(fileNameLabel);
//    mainLayout->addStretch(1);
//    setLayout(mainLayout);

    //int textSize = (int) (height * .1);
    QFont font("Helvetica [Cronyx]", 20);
    font.setPixelSize( 14 );

    infoText = new QTextEdit(this);
    infoText->setReadOnly(true);
    infoText->setText(dataToDisplay->getDataToDisplay());
    //infoText->setAttribute(Qt::WA_TranslucentBackground);
    //infoText->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); color: white;}" );//color : white; border: 0px solid black }");//border: 0px black; font: 16pt \"Helvetica\";");

#if defined(Q_OS_MAC )
    infoText->setStyleSheet( "QTextEdit{ background-color: rgba(255, 255, 255, 0); color : white; border: 0px black; font: 16pt \"Helvetica\";");
#endif
#if defined( Q_WS_WIN )
    infoText->setStyleSheet( "QTextEdit{ background-color: rgba(255, 255, 255, 0); color : white; border: 0px black; font: 16pt \"Helvetica\";");
#endif
#if defined( Q_OS_LINUX )
//    infoText->setAttribute(Qt::WA_TranslucentBackground);
//    infoText->setStyleSheet( "QTextEdit{ background-color: rgba(255, 0, 0); color : white; border: 0px black; font: 16pt \"Helvetica\";");
#endif
    infoText->setFont(font);
    //mainLayout->addItem(infoText);

}

//InfoTab::~InfoTab()
//{
//    if( mainLayout != NULL ) delete mainLayout;
//    if( infoText != NULL) delete infoText;
//}

ColorTab::ColorTab(GenomeElement* e, QColor currentColor, QString currentColorAppliedTo, QWidget *parent)
    : QWidget(parent), elt(e)
{
    //-----MAIN LAYOUT
    mainLayout = new QVBoxLayout;
    setLayout(mainLayout);

    //----TOP HORIZONTAL LAYOUT
    colorHorizontalLayout = new QHBoxLayout;
    //------ LABEL
    colorLabel = new QLabel;
    colorLabel->setText("Color:");
    colorLabel->setStyleSheet( " background-color: rgba(255, 255, 255, 0); color : white; font: 20pt \"Helvetica\"; ");
    colorHorizontalLayout->addWidget(colorLabel);
    //colorHorizontalLayout->setAlignment(colorLabel, Qt::AlignTop);

    //------- PIXMAP LABEL (SOLID-COLORED)
    colorPixmapLabel = new QLabel;
    colorPixmapLabel->setFixedWidth(20);
    colorPixmapLabel->setFixedHeight(20);
    QString style = "background-color: rgba(" + QString::number(currentColor.red()) + "," + QString::number(currentColor.green()) + "," + QString::number(currentColor.blue()) + "," + QString::number(currentColor.alpha()) + ")";
    colorPixmapLabel->setStyleSheet(style);
    colorHorizontalLayout->addWidget(colorPixmapLabel);
    //colorHorizontalLayout->setAlignment(colorPixmapLabel, Qt::AlignTop);

    //-------- SELECT NEW COLOR BUTTON
    selectNewColorButton = new QPushButton;
    selectNewColorButton->setText("Select New Color");
    connect(selectNewColorButton, SIGNAL(clicked()), this, SLOT(getNewColor()) );
    //selectNewColorButton->setStyleSheet( "QPushButton { border: 2px solid #8f8f91; border-radius: 6px; padding: 2px; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f6f7fa, stop: 1 #dadbde); min-width: 80px; };");
    colorHorizontalLayout->addWidget(selectNewColorButton);
    //colorHorizontalLayout->setAlignment(selectNewColorButton, Qt::AlignTop);
    mainLayout->addLayout(colorHorizontalLayout);

    //CAN'T USE COMBO BOX.  CRASHES PROGRAM...https://bugreports.qt-project.org/browse/QTBUG-29684
    //  Not sure how to fix this...
//    colorModeComboBox = new QComboBox;
//    colorModeComboBox->insertItem(0, "for all genes in cluster");
//    colorModeComboBox->insertItem(1, "for just this gene");
//    mainLayout->addWidget(colorModeComboBox);


    colorHorizontalLayout->addStretch(1);

}

//ColorTab::~ColorTab()
//{
//    if( colorDialog != NULL ) delete colorDialog;
//    if( mainLayout != NULL )delete mainLayout;
//    if( colorHorizontalLayout != NULL ) delete colorHorizontalLayout;
//    if( colorLabel != NULL ) delete colorLabel;
//    if( colorPixmapLabel != NULL) delete colorPixmapLabel;
//    if( selectNewColorButton != NULL ) delete selectNewColorButton;
//    if( colorModeComboBox != NULL ) delete colorModeComboBox;
//}

void ColorTab::getNewColor()
{
    QColorDialog colorDialog;

    QColor newColor = colorDialog.getColor();
    QString style = "background-color: rgba(" + QString::number(newColor.red()) + "," + QString::number(newColor.green()) + "," + QString::number(newColor.blue()) + "," + QString::number(newColor.alpha()) + ")";
    colorPixmapLabel->setStyleSheet(style);
    elt->setColor(newColor, true);
    elt->update();
}


//---------------------------------------------------------------------------------------------------------------------------------------
//POSITION TAB-------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
PositionTab::PositionTab(GenomeElement *elt, QWidget *parent)
    : QWidget(parent)
{
    //MAIN LAYOUT
    positionHorizontalLayout = new QVBoxLayout;
    setLayout(positionHorizontalLayout);

    //REMOVED SO WILL BE COMPATIBLE ON LINUX
    //was drawing info box too tall.  Possible that text is taller in linux, for some reason
    centerButton = new QPushButton;
//    centerButton->setText("Center this cluster");
//    positionHorizontalLayout->addWidget(centerButton);
//    connect( centerButton, SIGNAL(clicked()), elt, SLOT(emitCenteredSlot() ) );

    bringToTopButton = new QPushButton;
    bringToTopButton->setText("Move cluster to top");
    positionHorizontalLayout->addWidget(bringToTopButton);
    connect( bringToTopButton, SIGNAL(clicked()), elt, SLOT(emitMoveToTopSlot()) );


    alignButton = new QPushButton;
    alignButton->setText("Align cluster");
    connect( alignButton, SIGNAL(clicked()), elt, SLOT(emitAlignmentSlot()) );
    positionHorizontalLayout->addWidget(alignButton);

    allButton = new QPushButton;
    allButton->setText("Target this cluster"); //Center, align, and move cluster to top");
    connect( allButton, SIGNAL(clicked()), this, SLOT(emitTargetSignal()) );//SIGNAL(clicked()), elt, SLOT(emitTargetSlot()));
    connect( this, SIGNAL(targetSignal(QColor,QColor,QColor)), elt, SLOT(emitTargetSlot(QColor,QColor,QColor)) );
    positionHorizontalLayout->addWidget(allButton);

//    //---CENTER THIS LAYOUT
//    centerLayout = new QHBoxLayout;
//    positionHorizontalLayout->addLayout(centerLayout);
//    //-------CENTER THIS LABEL
//    centerThisLabel = new QLabel;
//    centerThisLabel->setText("Center ");
//    centerThisLabel->setStyleSheet( " background-color: rgba(255, 255, 255, 0); color : white; font: 20pt \"Helvetica\"; ");
//    centerLayout->addWidget(centerThisLabel);
//    //-------COMBO BOX
//    centerComboBox = new QComboBox;
//    centerComboBox->addItem("cluster");
//    centerComboBox->addItem("this gene");
//    centerComboBox->setStyleSheet("background-color: gray; border: 2px solid gray; color: white");
//    centerLayout->addWidget(centerComboBox);
//    //--------GO BUTTON
//    centerGoButton = new QPushButton;
//    centerGoButton->setText("GO!");
//    //connect(selectNewColorButton, SIGNAL(clicked()), this, SLOT(getNewColor()) );
//    centerLayout->addWidget(centerGoButton);



    //QHBoxLayout* bringToTopLayout;
}

int PositionTab::targetCount = 0;
void PositionTab::emitTargetSignal()
{
    //center color:  255.0, 208.0, 38.0, 255.0
    //right color:
        //leftColor.setRgb(13.0, 153.0, 73.0, 255.0);
    QColor startColor;
    QColor rightColor;
    QColor leftColor;
    int numTargetColorSchemes = 3;
    if( targetCount % 3 == 0 )
    {
        startColor.setRgb(255.0, 208.0, 38.0, 255.0);
        rightColor.setRgb(101.0, 88.0, 255.0);
        leftColor.setRgb(13.0, 153.0, 73.0, 255.0);
    }
    else if( targetCount % 3 == 1 )
    {
        startColor.setRgb(246.0, 236.0, 174.0);
        rightColor.setRgb(140.0, 8.0, 30.0);
        leftColor.setRgb(59.0, 41.0, 105.0);

    }
    else //if( targetCount % 3 == 2)
    {

        startColor.setRgb(46.0, 203.0, 174.0, 255.0);
        rightColor.setRgb(97.0, 41.0, 105.0, 255.0);
        leftColor.setRgb(8.0, 118.0, 140.0, 255.0);
//        startColor.setRgb(216.0, 152.0, 234.0);
//        rightColor.setRgb(114.0, 83.0, 212.0);
//        leftColor.setRgb(212.0, 83.0, 98.0);
    }
    //
    targetCount++;
    emit(targetSignal(leftColor,rightColor,startColor));
}

//---------------------------------------------------------------------------------------------------------------------------------------
//INFO BOX-------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------

InfoBox::InfoBox (  GenomeElement* e, GenomeElementDataPacket* d, int dir, QPointer<SageVis> sageVisPointer, VisBaseWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent)
{
    width = 300.0;
    height = 200.0;

    data = d;
    elt = e;

    direction = dir;

    init();
}

//InfoBox::~InfoBox()
//{
//    if( mainLayout != NULL ) delete mainLayout;
//    if( font != NULL ) delete font;
//    if( titleProxy != NULL) delete titleProxy;
//    //if( title != NULL ) delete title;
//    if( tabWidgetProxy != NULL )  delete tabWidgetProxy;
//    //if( tabWidget!= NULL ) delete tabWidget;
//    if( infoTab != NULL ) delete infoTab;
//    if( colorTab != NULL) delete colorTab;

//    if( mainLayout ) delete mainLayout;
//    if( font ) delete font;
//    if( titleProxy ) delete titleProxy;
//    if( title ) delete title;
//    if( tabWidgetProxy )  delete tabWidgetProxy;
//    if( tabWidget ) delete tabWidget;
//    if( infoTab ) delete infoTab;
//    if( colorTab ) delete colorTab;
//}

void InfoBox::init()
{
    //----CREATE OBJECTS
    mainLayout = new QGraphicsLinearLayout(this);

    setupShape();

    mainLayout->setOrientation(Qt::Vertical);

    int textSize = (int) (height * .1);
    font = new QFont("Helvetica [Cronyx]", textSize);
    font->setPixelSize( textSize );

    //----TITLE---
    title = new QLineEdit;
    titleProxy = new QGraphicsProxyWidget(this);
    titleProxy->setWidget(title);
    titleProxy->setAcceptHoverEvents(true);
    title->setReadOnly(true);
    QString toDisplay = data->getIdentifierToDisplay() ;
    titleProxy->setToolTip(toDisplay);
    if( toDisplay.size() > 35 )//if long, show '...' at end
    {
        title->setToolTip(toDisplay);//and show full text on hover //NOT WORKING.  WHY?
        //title->setAttribute(Qt::WA_AlwaysShowToolTips);
        toDisplay.replace(35, toDisplay.size()-35, "...");
    }
    title->setText(toDisplay );//getSeqId());
    title->setStyleSheet( " background-color: rgba(255, 255, 255, 0); color : white; border: 0px black;");
    title->setFont(*font);

    mainLayout->addItem(titleProxy);

    //---TAB WIDGET
    tabWidget = new QTabWidget;
    //tabWidget->setStyleSheet( " background-color: rgba(255, 255, 255, 0); " );
    tabWidget->setAttribute(Qt::WA_TranslucentBackground);
    //tabWidget->setStyleSheet( "QTabBar::tab{ background: rgba(0, 0, 0, 0) }");//"QTabWidget{ background-color: rgba(0, 0, 0, 0) }; QTabBar::tab{ background: rgba(0, 0, 0, 0) }");
    //tabWidget->setStyleSheet("QTabWidget{ background-color: rgb(0, 0, 0, 0) }");
//    tabWidget->setStyleSheet(
//                "QTabWidget::pane { border-top: 2px solid #C2C7CB; }; QTabWidget::tab-bar {left: 5px; }; QTabBar::tab { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E1E1E1, stop: 0.4 #DDDDDD, stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3); border: 2px solid #C4C4C3; border-bottom-color: #C2C7CB; border-top-left-radius: 4px; border-top-right-radius: 4px; min-width: 8ex; padding: 2px;}");

////                 QTabBar::tab:selected, QTabBar::tab:hover {
////                     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
////                                                 stop: 0 #fafafa, stop: 0.4 #f4f4f4,
////                                                 stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);
////                 }

////                 QTabBar::tab:selected {
////                     border-color: #9B9B9B;
////                     border-bottom-color: #C2C7CB; /* same as pane color */
////                 }

////                 QTabBar::tab:!selected {
////                     margin-top: 2px; /* make non-selected tabs look smaller */
////                }");
    tabWidgetProxy = new QGraphicsProxyWidget(this);
    tabWidgetProxy->setWidget(tabWidget);
    mainLayout->addItem(tabWidgetProxy);

    infoTab = new InfoTab(data);
    colorTab = new ColorTab(elt, elt->getColor(), "All in this cluster");
    positionTab = new PositionTab( elt );

    tabWidget->addTab(infoTab, tr("Info"));
    tabWidget->addTab(colorTab, tr("Color"));
    tabWidget->addTab(positionTab, tr("Position"));

    //setZValue(10000000.0);
}

void InfoBox::setupShape()
{

//    float x1 = boundingRect().x(); //+ .05*width;
//    float x2 = boundingRect().x() + .95*width;
//    float x3 = boundingRect().x() + .15*height;
//    float y1 = boundingRect().y() + .05*height;
//    float y2 = boundingRect().y() + height;
//    float y3 = boundingRect().y() + .75*height;

    if( direction == 0 )//draw up
    {
        trianglePt1.setX( 0.0 ); //x1 );//boundingRect().x() );
        trianglePt1.setY( height );//y2 );//height );

        trianglePt2.setX( 0.0 );//x1 );//boundingRect().x() );
        trianglePt2.setY( height-.07*height );//y3 );//height-.2*height );

        trianglePt3.setX( .10*height );//x3 );//boundingRect().x() + .15*height);
        trianglePt3.setY( height-.07*height );//y3 );//height-.2*height );

        //mainRect.setRect(boundingRect().x(), boundingRect().y(), width*.95, height*.85);
        //mainRect = new QGraphicsRectItem(this);
        mainRect.setRect(0.0, 0.0, (width-10.0), height*.98);//boundingRect().x(), boundingRect().y(), width*.95, height*.85);
        //QColor backgroundColor;
        //    backgroundColor.setRgb(60.0, 60.0, 60.0, 255.0);
        //    mainRect->setBrush(backgroundColor);
        //    mainRect->setPen( Qt::NoPen );

        //    mainLayout->setMaximumWidth(mainRect.width());
        //    mainLayout->setMaximumHeight(mainRect.height());

        mainLayout->setGeometry(mainRect);
    }
    else if( direction == 1 )
    {
        trianglePt1.setX( 0.0 ); //x1 );//boundingRect().x() );
        trianglePt1.setY( 0.0 );//height );//y2 );//height );

        trianglePt2.setX( 0.0 );//x1 );//boundingRect().x() );
        trianglePt2.setY( .07*height );//height-.07*height );//y3 );//height-.2*height );

        trianglePt3.setX( .10*height );//x3 );//boundingRect().x() + .15*height);
        trianglePt3.setY( .07*height );//height-.07*height );//y3 );//height-.2*height );

        //mainRect.setRect(boundingRect().x(), boundingRect().y(), width*.95, height*.85);
        //mainRect = new QGraphicsRectItem(this);
        mainRect.setRect(0.0, .02*height, (width-10.0), height); //height*.98);//boundingRect().x(), boundingRect().y(), width*.95, height*.85);
        //QColor backgroundColor;
        //    backgroundColor.setRgb(60.0, 60.0, 60.0, 255.0);
        //    mainRect->setBrush(backgroundColor);
        //    mainRect->setPen( Qt::NoPen );

        //    mainLayout->setMaximumWidth(mainRect.width());
        //    mainLayout->setMaximumHeight(mainRect.height());

        mainLayout->setGeometry(mainRect);
    }
}

void InfoBox::setGeometry(const QRectF &geom)
{
   width = geom.width();
   height = geom.height();

   super::setGeometry(geom);
   setupShape();
}

void InfoBox::setGeometry(qreal x, qreal y, qreal w, qreal h)
{
    width = w;
    height = h;
    super::setGeometry(x, y, w, h);
    setupShape();
}

void InfoBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
   GenomeElement* p = dynamic_cast<GenomeElement*> (parentItem());
   //if( p->geometry().contains(mapToItem(p, event->pos())))
   //    qDebug() << "*******leaving into gene*****" << endl;
//   else
//       p->hovering = false;
//   p->hoverEnterEvent(event);//processHoverEvent( event );
   //p->hovering = true;
   //p->update();

//   oldColor.setRgb(p->givenColor.red(), p->givenColor.green(), p->givenColor.blue(), p->givenColor.alpha() );
//   p->setColor(p->highlightColor);

   super::hoverEnterEvent(event);

}

void InfoBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
   GenomeElement* p = dynamic_cast<GenomeElement*> (parentItem());
//   if( p->geometry().contains(mapToItem(p, event->pos())))
//   {
//       qDebug() << "*******leaving into gene*****" << endl;
//       p->hoverEnterEvent(event);
//   }
//  else
//   {
////       p->hovering = false;
//   //p->hovering = false;
//    //p->hoverLeaveEvent(event);
//       //p->processHoverEvent( event );
//   }
    //p->hovering = false;
    //p->update();
   //p->setColor( oldColor );
   super::hoverLeaveEvent(event);

}

//void InfoBox::


QRectF InfoBox::boundingRect() const
{
    return QRectF( -10.0, 0.0, width, height);
}

QPainterPath InfoBox::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void InfoBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor backgroundColor;
    backgroundColor.setRgb(60.0, 60.0, 60.0, 230.0);//255.0);
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    //QRectF bRect = boundingRect();
    //bRect.adjust(0, 1, 0, -1);
    painter->drawRoundedRect(mainRect, .05*height, .05*height);

    //painter->setBrush(Qt::blue);
    QPolygonF poly;
    poly << trianglePt1 << trianglePt2 << trianglePt3;
    painter->drawPolygon(poly);

}
